<template>
  <div>
    <sticky class-name="sub-navbar white">
      <el-button style="margin:5px 10px;float:left" type="text" icon="el-icon-arrow-left" @click="handleReturn" >返回</el-button>
      <div>
        <el-button :loading="loading" style="margin-left: 10px;" type="primary" @click="handleSave">保存</el-button>
      </div>
    </sticky>

    <div style="margin:10px">
      <el-row >
        <el-col :span="12" :xs="24" :md="12" :lg="12">
          <el-card style="margin:10px">
            <div slot="header" class="clearfix">
              <span>资助人信息</span>
            </div>
            <el-form ref="formBaseInfo" :model="form" :rules="rules" label-width="100px" size="mini">
              <el-form-item label="姓名" prop="name">
                <el-input v-model="form.name" :max-length="20" style="width:calc(100% - 60px)"/>
                <el-checkbox v-model="form.anonymous" style="margin-right:0px;">匿名</el-checkbox>
              </el-form-item>
              <el-form-item label="昵称" prop="nickname">
                <el-input v-model="form.nickname" :max-length="20"/>
              </el-form-item>

              <el-form-item label="类别" prop="type">
                <el-select v-model="form.type" style="width:100%">
                  <el-option v-for="item in sponsorTypes" :key="item.code" :label="item.name" :value="item.code"/>
                </el-select>
              </el-form-item>
              <el-form-item label="性别" prop="gender">
                <el-select v-model="form.gender" style="width:100%">
                  <el-option v-for="item in genders" :key="item.code" :label="item.name" :value="item.code"/>
                </el-select>
              </el-form-item>
              <el-form-item label="生日" prop="birthday">
                <el-date-picker v-model="form.birthday" type="date" style="width:100%"/>
              </el-form-item>
              <el-form-item label="所在地区" prop="area">
                <el-input v-model="form.area" :max-length="20"/>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
        <el-col :span="12" :xs="24" :md="12" :lg="12">
          <el-card style="margin:10px">
            <div slot="header" class="clearfix">
              <span>联系方式</span>
            </div>
            <el-form ref="formContact" :model="form" :rules="rules" label-width="100px" size="mini">
              <el-form-item label="电话" prop="contactPhone">
                <el-input v-model="form.contactPhone" :max-length="30"/>
              </el-form-item>
              <el-form-item label="微信" prop="contactWechat">
                <el-input v-model="form.contactWechat" :max-length="30"/>
              </el-form-item>
              <el-form-item label="Email" prop="contactEmail">
                <el-input v-model="form.contactEmail" :max-length="30"/>
              </el-form-item>
              <el-form-item label="地址" prop="contactAddress">
                <el-input v-model="form.contactAddress" :max-length="50"/>
              </el-form-item>
            </el-form>
          </el-card>
        </el-col>
      </el-row>

      <el-row>
        <el-card style="margin:10px">
          <div slot="header" class="clearfix">
            <span>备注</span>
          </div>
          <div>
            <el-form ref="formDescription" :model="form" :rules="rules" size="mini">
              <el-form-item label="" prop="remark">
                <el-input v-model="form.remark" :maxlength="200" autosize size="small" type="textarea"/>
              </el-form-item>
            </el-form>
          </div>
        </el-card>
      </el-row>
      <el-row>
        <el-card style="margin:10px">
          <div slot="header" class="clearfix">
            <span>资助信息</span>
          </div>
          <el-form ref="formPoverty" :model="form" :rules="rules" label-width="160px" size="mini">
            <el-row>
              <el-col :span="12" >
                <el-form-item label="排队等待资助" prop="waitPair">
                  <el-checkbox v-model="form.waitPair"/>
                </el-form-item>
              </el-col>
              <el-col :span="12" >
                <el-form-item label="希望参加暑期走访" prop="wantVisit">
                  <el-checkbox v-model="form.wantVisit"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-form>
        </el-card>
      </el-row>
    </div>

  </div>
</template>

<script>
import { add } from '@/api/sponsors'
import { getSponsorTypes, getGenders } from '@/api/public'
import waves from '@/directive/waves' // 水波纹指令
import Sticky from '@/components/Sticky'
import { getLines, parseTime } from '@/utils'

export default {
  name: 'SponsorsAdd',
  directives: {
    waves
  },
  components: { Sticky },
  filters: {
  },

  props: {
    // id: {
    //   type: Number,
    //   default: null
    // }
  },
  data() {
    return {
      loading: false,
      genders: [],
      sponsorTypes: [],
      form: {
        name: null,
        gender: null,
        nickname: null,
        anonymous: false,
        area: null,
        birthday: null,
        remark: null,

        contactPhone: null,
        contactWechat: null,
        contactEmail: null,
        contactOther: null,
        contactAddress: null,

        type: null,
        waitPair: false,
        wantVisit: false
      },
      contactsList: [],
      listLoading: true,
      queryInfo: null,

      rules: {
        name: [{ required: true, message: '不能为空' }],
        gender: [{ required: true, message: '不能为空' }],
        nickname: [{ required: true, message: '不能为空' }],
        anonymous: [{ required: true, message: '不能为空' }],
        area: [{ required: true, message: '不能为空' }],
        birthday: [{ required: true, message: '不能为空' }],

        type: [{ required: true, message: '不能为空' }],
        waitPair: [{ required: true, message: '不能为空' }],
        wantVisit: [{ required: true, message: '不能为空' }]
      }

    }
  },

  computed: {
    headerStyle() {
      // return {
      //   'border': '1px solid #ebeef5',
      //   'border-bottom': '0px solid #ebeef5'
      // }
      return {
        'border': '0px solid #ebeef5',
        'border-bottom': '1px solid #ebeef5'
      }
    }
  },
  watch: {
  },
  created() {
    this.getPublicData()
  },
  methods: {
    handleReturn() {
      this.$emit('backClick')
    },
    getPublicData() {
      this.getSponsorTypes()
      this.getGenders()
    },
    getGenders() {
      getGenders().then((res) => {
        this.genders = res.data
      })
    },
    getSponsorTypes() {
      getSponsorTypes().then((res) => {
        this.sponsorTypes = res.data
      })
    },
    handleSave() {
      this.validateForm().then((valid) => {
        if (valid) {
          if (this.form.birthday) {
            this.form.birthday = parseTime(this.form.birthday, '{y}-{m}-{d}')
          }
          add(this.form).then((res) => {
            this.$notify({
              message: '添加成功',
              type: 'success',
              duration: 2000
            })
            this.handleReturn()
          })
        }
      })
    },
    validateForm() {
      return new Promise((resolve, reject) => {
        const nameList = []
        nameList.push('formBaseInfo')
        nameList.push('formContact')
        nameList.push('formDescription')
        nameList.push('formPoverty')

        const plist = []
        for (const i in nameList) {
          const p = new Promise((resolve, reject) => {
            this.$refs[nameList[i]].validate((valid) => {
              if (valid) {
                resolve()
              } else {
                reject()
              }
            })
          })
          plist.push(p)
        }
        Promise.all(plist).then(() => {
          resolve(true)
        }, () => {
          resolve(false)
        })
      })
    },
    getLines(str) {
      return getLines(str)
    }

  }
}

</script>

<style lang="scss" scoped>

  .datatable-header{
      height: 40px;
      width: 100%;
      background: white;
      padding: 12px 16px;
      /* border: 1px solid #ebeef5;
      border-bottom: 1px solid #ebeef5; */
      line-height: 1;
      .header-text{
        font-size: 18px;
        font-weight: bold;
        color:#303133;;
      }
  }
</style>

